define(['knockout', 'aForm', 'config'], function (ko, Form, Config) {//form field end with button inputs
    function ViewModel(params) {
        var scope = this;
        scope.labelDirection = 'center';
        scope.callParent(params);

        var baseObj = {type: 'button', alias: '', _colCss: {}, _attr: {}, _labelCss: {},_inputCss:{}};

        if (scope.formButtons) {
            for(var bIndex = 0;bIndex<scope.formButtons.length;++bIndex){
                var tempButton = $.extend({
                    value: scope.formButtons[bIndex].value,
                    name: scope.formButtons[bIndex].name,
                    __inputCss: Config.buttonCss.primary
                }, baseObj)

                if(scope.buttonPosition) {
                    scope._fieldSets[0]._fields.splice(scope.buttonPosition+bIndex,0,tempButton);
                }
                else {
                    scope._fieldSets[0]._fields.push(tempButton);
                }
            }
        } else {
            var queryButton = $.extend({
                value: '查询',
                name: 'primary',
                __inputCss: Config.buttonCss.primary
            }, baseObj);
            var clearButton = $.extend({
                value: '清空',
                name: 'secondary',
                __inputCss: Config.buttonCss.secondary
            },baseObj);

            if(scope.buttonPosition){
                scope._fieldSets[0]._fields.splice(scope.buttonPosition,0,queryButton,clearButton);
            }
            else {
                scope._fieldSets[0]._fields.push(queryButton);
                scope._fieldSets[0]._fields.push(clearButton);
            }
        }


        scope._onFormButtonClick = function (btn) {
            var store = params.store?params.store:ko.root.storeCache[scope.record.getStoreId()];

            switch (btn.value) {
                case '查询':
                    if (store) {
                        store.query = {};
                        store.query = scope.record.getFormJsonData();
                        store.empty();
                        store.load();
                    }
                    break;
                case '清空':
                    scope.record.revert();
                    break;
            }

            if (scope.onFormButtonClick) {
                scope.onFormButtonClick({record: scope.record, name: btn.name});
            }
        };

    }

    ViewModel.extend(Form);

    return ViewModel;
});
